package com.gxy.learn.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
public class TestWebClientController {

    @Autowired
    private WebClient.Builder webClientBuilder;


    @Bean
    @LoadBalanced
    private WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }

    /**
     * 利用spring.application.name来调用接口
     * @return
     */
    @GetMapping("/webClient/test")
    public String test() {
        // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
        String springApplicationName = "nacos-provider-server";
        String url = String.format("http://%s/hello?name=gaoxueyong",springApplicationName);
        Mono<String> restlut = webClientBuilder.build().get().uri(url).retrieve().bodyToMono(String.class);
        log.info("springApplicationName = {}",springApplicationName);
        String retRes = String.format("return : %s", restlut.block());
        log.info(retRes);
        return retRes;
    }


}
